package com.loongtech.bi.action.newsystem;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityConfig;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.bi.manager.system.SyncInfoLogManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Author: yuri
 * Description: 系统配置
 * Date: 16:19 2017/5/23
 */
@RestController
@Scope("prototype")
@RequestMapping("/sysconfig")
public class SysConfigAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private ConfigManager configManager;
    @Resource
    private SyncInfoLogManager syncInfoLogManager;


    /**
     * 查询
     */
    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> searchByCriteria(@RequestParam Integer curPage, @RequestParam Integer pageSize, EntityConfig entityConfig) {
        Page<List<EntityConfig>> list;
        try {
            list = configManager.queryByCriteria(curPage, pageSize, entityConfig);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> add(EntityConfig entity) {
        if (entity.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyGameId.getErrorcode(), RetCode.eEmptyGameId.getErrorMsg());
        }
        try {
            EntityConfig res = configManager.getByQuery("from EntityConfig where gameId = ? and enumName = ?",entity.getGameId(), entity.getEnumName());
            if (res != null) {
                return new ResultData<>(RetCode.eNameExist.getErrorcode(), RetCode.eNameExist.getErrorMsg());
            }
            configManager.persist(entity);
            StringBuilder insertSQL = new StringBuilder("insert into system_config(gameId,enumName,enumValue,description,version) values");
            insertSQL.append("(").append("'").append(entity.getGameId()).append("','").append(entity.getEnumName()).append("','").append(entity.getEnumValue()).append("','").append(entity.getDescription()).append("',1)");
            String gameId = entity.getGameId();
            for (String gid : gameId.split(",")) {
                syncInfoLogManager.add(gid, insertSQL.toString());
            }
            sysLogManager.addLog(session.getUserName(), "系统配置增加成功(" + entity.getEnumName() + ")");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(EntityConfig entity) {
        if (entity.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyGameId.getErrorcode(), RetCode.eEmptyGameId.getErrorMsg());
        }
        try {
            EntityConfig cur = configManager.get(entity.getId());
            if (cur != null) {
                configManager.merge(entity);
                StringBuilder updateSQL = new StringBuilder("update system_config set enumName = ");
                updateSQL.append("'").append(entity.getEnumName()).append("',enumValue = '").append(entity.getEnumValue()).append("',description = '").append(entity.getDescription()).append("' where  enumName = '").append(entity.getEnumName()).append("'");
                String gameId = entity.getGameId();
                for (String gid : gameId.split(",")) {
                    syncInfoLogManager.add(gid, updateSQL.toString());
                }
                sysLogManager.addLog(session.getUserName(), "系统配置修改成功:" + entity.getEnumName());
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNotExistGourp.getErrorcode(), RetCode.eNotExistGourp.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}
